#pragma once

#include <WinInet.h>

typedef enum
{
	HTTP_METHOD_GET,
	HTTP_METHOD_POST
} HttpMethod;

class Http
{
public:
	Http(void);
	~Http(void);

	BOOL Connect(LPCTSTR lpszServerName, INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTP_PORT,BOOL bIsSSL = FALSE, LPCTSTR lpszProxy = NULL, LPCTSTR lpszProxyBypass = NULL);
	void Close();

	BOOL GetCookie(HINTERNET hRequest, LPCTSTR lpszCookie, LPDWORD pdwCookieLen, LPDWORD pdwIndex);
	BOOL GetCookie(HINTERNET hRequest, LPCTSTR lpszCookie);

	BOOL DoGet (LPCTSTR lpszPath, LPTSTR *lpszBuffer, LPDWORD dwStatus, LPCTSTR lpszCookie);
	BOOL DoPost(LPCTSTR lpszPath, LPSTR lpszData, LPCTSTR * lpszHttpHeaders,int nHttpHeadersCount, LPTSTR *lpszBuffer, LPDWORD dwStatus, LPCTSTR lpszCookie);

	int GetError() { return m_nLastError; }
private:

	BOOL CreateSession(LPCTSTR lpszProxy = NULL, LPCTSTR lpszProxyBypass = NULL);
	void CloseSession();
	BOOL RecreateSession(LPCTSTR lpszProxy = NULL, LPCTSTR lpszProxyBypass = NULL);

	HINTERNET CreateRequest(HINTERNET hConnect, LPCTSTR lpszPath, HttpMethod method);
	void CloseRequest(HINTERNET hRequest);

	BOOL GetResponse(HINTERNET hRequest, LPTSTR *lpszBuffer, LPDWORD pdwStatus);

	HINTERNET	m_hSession;
	HINTERNET	m_hConnect;
// 	HINTERNET	m_hRequest;

	int		m_nLastError;
	BOOL	m_bSSL;
	BOOL	m_SessionCreated;
};
